
#include "player.h"
#include "log.h"
#include "helpers.h"

#include <mpd/client.h>

#include <string>
#include <iostream>
#include <list>

using namespace std;

int currentVolume = 0;


struct mpd_connection* getMPDConnection() {
    
    struct mpd_connection* conn = mpd_connection_new(NULL, 0, 30000);

    if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS){
        Log::Warning("MPD not connected");
        return NULL;
    }
    return conn;
}

void closeMPDConnection(struct mpd_connection* conn){
    mpd_connection_free(conn);
}

void shairport_on(){

    struct mpd_connection* conn = getMPDConnection();

    if(!mpd_run_pause(conn,1)){
        Log::Warning("MPD Pause command didnt work.");
    }

    mpd_response_finish(conn);

    Log::Message( "Starting Shairport");
    system("shairport&");

    closeMPDConnection(conn);

}

void shairport_off(){
    Log::Message("Stopping Shairport");
    system("sudo killall shairport");
}


void playlist_play() {

    struct mpd_connection* conn = getMPDConnection();

    mpd_run_play(conn);

    closeMPDConnection(conn);
}

void playlist_pause() {

    struct mpd_connection* conn = getMPDConnection();
    mpd_run_pause(conn,1);
    closeMPDConnection(conn);

}

void playlist_load_radios(){

    shairport_off();

    struct mpd_connection* conn = getMPDConnection();
    if(!mpd_run_clear(conn)) { Log::Warning("Current playlist not cleared.");}
    if(!mpd_run_load(conn, PATH_TO_RADIO)) { Log::Warning("Failed to load PATH_TO_RADIO");}
    if(!mpd_run_repeat(conn,1)) {Log::Warning("Failed to put in repeat mode.");}
    if(!mpd_run_play(conn)) { Log::Warning("Failed to play.");}
    closeMPDConnection(conn);


}

void playlist_stop(){

    struct mpd_connection* conn = getMPDConnection();
    mpd_run_stop(conn);
    closeMPDConnection(conn);

}

void playlist_next(){

    struct mpd_connection* conn = getMPDConnection();
    mpd_run_next(conn);
    closeMPDConnection(conn);

}

void playlist_prev(){

    struct mpd_connection* conn = getMPDConnection();

    mpd_run_previous(conn);

    closeMPDConnection(conn);

}

void setVolume(int volume) {
     struct mpd_connection* conn = getMPDConnection();

     if(volume > 100) 
        volume = 100;
     else if(volume < 0)
         volume = 0;

     currentVolume = volume;

     if(!mpd_send_set_volume(conn, volume)) { Log::Message("Failed Volume Set to "+ Helpers::toString(currentVolume));}

     cout << "currentVolume " << Helpers::toString(currentVolume) << endl;


     closeMPDConnection(conn);

}

void stepVolume(int step){
    cout << "step :" << Helpers::toString(step) << endl;

    setVolume(currentVolume+step);
}

void playlist_load_mp3(){

    shairport_off();

    struct mpd_connection* conn = getMPDConnection();

    list<string> songs_URI;

    if(!mpd_run_clear(conn)) { Log::Warning("Playlist failed to be cleared.");}

    if(!mpd_send_list_all(conn, "")){Log::Warning("Failed listing all the songs in the DB ");}

    mpd_song *song;

    while ((song = mpd_recv_song(conn)) != NULL) {
        string str = mpd_song_get_uri(song);
        songs_URI.push_back(str);
        mpd_song_free(song);
    }

    for(list<string>::iterator list_iter = songs_URI.begin();list_iter != songs_URI.end(); list_iter++)
    {
        mpd_run_add(conn,list_iter->c_str());
    }

    mpd_response_finish(conn);
    mpd_run_random(conn,1);
    mpd_run_repeat(conn,1);
    mpd_run_play(conn);

    closeMPDConnection(conn);

}

